//
//  KLServiceCenter.m
//  KoalaUtilKit
//
//  Created by Tommy on 15/1/21.
//  Copyright (c) 2015年 Koala Team. All rights reserved.
//

#import "KLRegisterCenter.h"

@interface KLRegisterCenter()

@property(nonatomic,strong)NSMutableDictionary *center;
@end


@implementation KLRegisterCenter
KLDEF_SINGLETON(KLRegisterCenter);

- (instancetype)init{
    if(self = [super init]){
        _center = [[NSMutableDictionary alloc]init];
    }
    return self;
}

- (void)register:(id)object forKey:(Protocol*)key{
    NSAssert([object conformsToProtocol:key],@"%@ not conforms protocol:%@",object,key);
    NSAssert([[object class]respondsToSelector:@selector(sharedInstance)],@"%@ class don't has sharedInstance",[object class]);
    
    @synchronized(self){
        _center[NSStringFromProtocol(key)] = object;
    }
}
- (id)removeObjectForKey:(Protocol*)key{
    NSString *strKey = NSStringFromProtocol(key);
    id object = _center[strKey];
    @synchronized(self){
        [_center removeObjectForKey:strKey];
    }
    return object;
}
- (id) objectForKey:(Protocol*)key{
    return _center[NSStringFromProtocol(key)];
}

- (BOOL)hasObjectForKey:(Protocol*)key{
    return _center[key] != nil;
}

- (NSString*)ProtocolKey:(Protocol*)key{
    return NSStringFromProtocol(key);
}


@end
